package com.jml.servlet.web;

import java.lang.reflect.Method;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;


@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    /**
     * onStartup servlet容器初始化的时候就会调用该方法
     *
     * @param classInfos 获取 WebApplicationInitializer 所有的子类
     * @param ctx
     * @throws ServletException
     */
    @Override
    public void onStartup(Set<Class<?>> classInfos, ServletContext ctx) throws ServletException {

        for (Class<?> classInfo : classInfos) {
            //classInfo 都是WebApplicationInitializer类的子类
            try {
                Method method = classInfo.getMethod("onStartup", ServletContext.class);
                Object object = classInfo.newInstance();
                method.invoke(object, ctx);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
